<template>
  <div class="login">
    <div class="box">
      <h1 align="center">梦学谷会员管理系统</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="55px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
              this.$store.dispatch('doLogin',this.ruleForm).then(res=>{
                  if(res.data.flag){
                      this.$message.success(res.data.message)
                      this.$router.push('/layout')
                  }else{
                      this.$message.error(res.data.message)
                  }
              }).catch(error=>{
                  console.log(error);
              })
        } else {
            this.$message.error('输入内容有误')
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg") no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  .box {
    width: 350px;
    padding: 30px;
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 20px;
    h1 {
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 20px;
    }
  }
}
</style>
